(function($){
    $.fn.chartMap = function(options){
        var $this = this;
        var charts = echarts.init(this[0]);
        var defaults = {
            visualMap: {
                min: 1,
                max: 1000,
                inRange: {
                    color: ['#313695', '#4575b4', '#74add1', '#abd9e9', '#e0f3f8', '#ffffbf', '#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026']
                },
                calculable: true,
                show:false
            },

            series: [{
                type: 'map',
                map: 'guizhou',
                data:[]
            }]
        };
        var opts = $.extend(defaults,options);
        var map = {
            setData:function(data){
                if(data.url == undefined || data.url == ''){
                    throw 'url is undefined '
                }
                if(!data.url.endsWith(".json")) {
                    throw 'Incorrect file format '
                }
                if(data.name == undefined || data.name == ''){
                    throw 'name is undefined '
                }
                $.get(data.url, function (chinaJson) {
                    echarts.registerMap(data.name, chinaJson);
                    var $data = data.data;
                    if($data.length > 0){
                        if("number" == typeof($data[0])){
                            $data = chinaJson.features.map(function(o,i){
                                var value = 0;
                                if(i < $data.length) {
                                    value = $data[i];
                                }
                                return {name:o.properties.name,value:value}
                            })
                        }
                    }
                    opts.series[0].data = $data;
                    opts.series[0].map = data.name;
                    charts.setOption(opts);
                },"json");
            },
            on:function(event,func){
                charts.on(event,func);
            },
            setOption:function(ops){
                charts.setOption($.extend(true,charts.getOption(), ops));
            },
            getCharts:function(){
                return charts;
            },
            resize:function(){
                charts.resize();
            }
        }
        return map;
    }
})(jQuery)